﻿//using Mathd;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GisPointTo3DPoint : MonoBehaviour
{
    public static GisPointTo3DPoint Instance;
    public Camera MC;
    //public EnviroCore EC;
    [Tooltip("Unity中左上点")]
    public Transform topLeftPoint;
    [Tooltip("Unity中右下点 ")]
    public Transform bottomRightPoint;
    [Tooltip("地图中对应的左上经纬度点 ")]
    public double topLeftSaijingdu;
    public double topLeftSaiweidu;
    [Tooltip("地图中对应的右下经纬度点 ")]
    public double bottomRightSaijingdu;
    public double bottomRightSaiweidu;
    public Vector2d topLeftSai;
    public Vector2d bottomRightSai;

    internal bool init_bool;
    //public Transform Target;
    private double z_offset, x_offset, z_w_offset, x_w_offset;

    // test
    public double longitude;
    public double latitude;
    public double distance;
    public Transform obj;
    public Transform obj1;
    private void Awake()
    {
        Instance = this;
        MC = Camera.main;
        //topLeftSai = new Vector2d(topLeftSaijingdu, topLeftSaiweidu);
        //bottomRightSai = new Vector2d(bottomRightSaijingdu, bottomRightSaiweidu);
        ////else if (Instance != this) Destroy(this);
        //InitBasicNum();//初始化参数    
        //NetDataTool.post += FindNeighPosition;
    }
    public void InitBasicNum()
    {

        if (topLeftPoint != null && bottomRightPoint != null)
        {
            topLeftSai = new Vector2d(topLeftSaijingdu, topLeftSaiweidu);
            bottomRightSai = new Vector2d(bottomRightSaijingdu, bottomRightSaiweidu);
            z_offset = ABS(topLeftSai.y - bottomRightSai.y);         //地图中的维度差 
            x_offset = ABS(topLeftSai.x - bottomRightSai.x);         //地图中的经度差   
            z_w_offset = ABS(topLeftPoint.localPosition.z - bottomRightPoint.localPosition.z);
            //unity中的维度差
            x_w_offset = ABS(topLeftPoint.localPosition.x - bottomRightPoint.localPosition.x);
            //unity中的经度差 
        }
    }
    /// <summary>
    /// 由经纬度得到位置点 
    /// </summary>
    /// <param name="se">经纬度</param>
    /// <returns></returns>
    Vector3 getWorldPoint(Vector2d se, Transform Model)
    {
        if (topLeftPoint != null && bottomRightPoint != null)
        {
            //InitBasicNum();
            if ((se.x >= topLeftSai.x &&
               se.x <= bottomRightSai.x
               &&
               se.y <= bottomRightSai.y &&
               se.y >= topLeftSai.y) ||
               (se.x >= topLeftSai.x &&
               se.x <= bottomRightSai.x
               &&
               se.y >= bottomRightSai.y &&
               se.y <= topLeftSai.y)
               ||
               (se.x <= topLeftSai.x &&
               se.x >= bottomRightSai.x
               &&
               se.y >= bottomRightSai.y &&
               se.y <= topLeftSai.y)
               ||
               (se.x <= topLeftSai.x &&
               se.x >= bottomRightSai.x
               &&
               se.y <= bottomRightSai.y &&
               se.y >= topLeftSai.y)
                )
            {
                double tempX = ABS(se.x - bottomRightSai.x);
                double tempZ = ABS(se.y - topLeftSai.y);
                double _tempX = (tempX * x_w_offset / x_offset + bottomRightPoint.localPosition.x);
                double _tempZ = (tempZ * z_w_offset / z_offset + topLeftPoint.localPosition.z);
                return new Vector3((float)(_tempX + Model.position.x) * this.transform.lossyScale.x, 0f, (float)(_tempZ + Model.position.z) * this.transform.lossyScale.z);
            }
            else
                return new Vector3(-100, -100, -100);
        }
        else
            return new Vector3(-100, -100, -100);
    }

    public Vector3 GetWorldPoint(double x, double y)//经纬度转化成坐标
    {
        InitBasicNum();
        double tempX = x - topLeftSai.x;
        double tempZ = y - bottomRightSai.y;
        float _tempX = (float)(tempX * x_w_offset / x_offset + topLeftPoint.position.x);
        float _tempZ = (float)(tempZ * z_w_offset / z_offset + bottomRightPoint.position.z);
        //坐标偏差
        return new Vector3(_tempX, 0, _tempZ);
    }

    /// <summary>
    /// 由位置点得到经纬度  
    /// </summary>
    /// <param name="curPoint">位置</param>
    /// <returns></returns>
    public Vector2d getLatLon(Transform Obj)//坐标转化成经纬度
    {
        if (topLeftPoint != null && bottomRightPoint != null)
        {
            Vector3 curPoint = Obj.localPosition;
            InitBasicNum();
            double _z_offset = ABS((double)(curPoint.z / Obj.localScale.z) - bottomRightPoint.localPosition.z) * z_offset / z_w_offset;
            double _x_offset = ABS((double)(curPoint.x / Obj.localScale.x) - topLeftPoint.localPosition.x) * x_offset / x_w_offset;
            double resultX = _x_offset + topLeftSai.x;
            double resultZ = _z_offset + bottomRightSai.y;
            return new Vector2d(resultX, resultZ);
        }
        else
        {
            return new Vector2d(100, 100);
        }
    }

    /// <summary>
    /// 根据一个给定经纬度的点和距离，进行附近地点查询
    /// </summary>
    /// <param name="longitude">经度</param>
    /// <param name="latitude">纬度</param>
    /// <param name="distance">距离（单位：公里或千米）</param>
    /// <returns>返回一个范围的4个点，最小纬度和纬度，最大经度和纬度</returns>
    [ContextMenu("testFindNeigh")]
    public void FindNeighPosition()//double longitude, double latitude, double distance)
    {
        //先计算查询点的经纬度范围  
        double r = 6378.137;//地球半径千米  
        double dis = distance;//千米距离    
        double dlng = 2 * Math.Asin(Math.Sin(dis / (2 * r)) / Math.Cos(latitude * Math.PI / 180));
        dlng = dlng * 180 / Math.PI;//角度转为弧度  
        double dlat = dis / r;
        dlat = dlat * 180 / Math.PI;
        bottomRightSaiweidu = latitude - dlat;
        topLeftSaiweidu = latitude + dlat;
        topLeftSaijingdu = longitude - dlng;
        bottomRightSaijingdu = longitude + dlng;
        topLeftSai = new Vector2d(topLeftSaijingdu, topLeftSaiweidu);
        bottomRightSai = new Vector2d(bottomRightSaijingdu, bottomRightSaiweidu);
        InitBasicNum();


        Debug.Log(getLatLon(obj1));
        Debug.Log(GetWorldPoint(85.0166611111111, 8));
        //return new PositionModel
        //{
        //    MinLat = minlat,
        //    MaxLat = maxlat,
        //    MinLng = minlng,
        //    MaxLng = maxlng
        //};
    }
    public void FindNeighPosition(double longitude, double latitude)
    {
        //先计算查询点的经纬度范围  
        double r = 6378.137;//地球半径千米  
        double dis = 90;//千米距离    
        double dlng = 2 * Math.Asin(Math.Sin(dis / (2 * r)) / Math.Cos(latitude * Math.PI / 180));
        dlng = dlng * 180 / Math.PI;//角度转为弧度  
        double dlat = dis / r;
        dlat = dlat * 180 / Math.PI;
        bottomRightSaiweidu = latitude - dlat;
        topLeftSaiweidu = latitude + dlat;
        topLeftSaijingdu = longitude - dlng;
        bottomRightSaijingdu = longitude + dlng;
        topLeftSai = new Vector2d(topLeftSaijingdu, topLeftSaiweidu);
        bottomRightSai = new Vector2d(bottomRightSaijingdu, bottomRightSaiweidu);
        InitBasicNum();
        init_bool = true;

        Debug.Log("基准点校准成功"+ init_bool);
        //Debug.Log("topLeftSai:" + topLeftSai);
        //Debug.Log("bottomRightSai:" + bottomRightSai);
        //return new PositionModel
        //{
        //    MinLat = minlat,
        //    MaxLat = maxlat,
        //    MinLng = minlng,
        //    MaxLng = maxlng
        //};
    }

    public double ABS(double a)
    {
        if (a > 0)
            return a;
        else
            return -a;

    }

    void SetCameraLatLon()
    {
        //EC.GameTime.utcOffset = 8;
        //EC.GameTime.Latitude = (float)getLatLon(MC.transform).y;
        //EC.GameTime.Longitude = (float)getLatLon(MC.transform).x;
    }
    private void Update()
    {
        SetCameraLatLon();
    }
   
}
public class PositionModel
{
    public double MinLat;
    public double MaxLat;
    public double MinLng;
    public double MaxLng;
}
public struct Vector2d 
{
    public double x;
    public double y;
    public Vector2d(double p_x, double p_y)
    {
        x = p_x;
        y = p_y;
    }
}

